var backupload=false;
function getptt(rank,num,mxp,mis,far)
{
	var pur=num-mis-far;
	var score=Math.floor(1e7*(pur+far/2)/num+mxp);
	var ptt=Math.max(0,rank+Math.min((score>=9800000?(score-9600000)/200000:(score-9500000)/300000),2));
	return ptt;
}
function uploadplay()
{
	var title=document.getElementById('songtitle').value;
	var diff=null;var obj=document.getElementsByName("diff");
	for(var i in obj) if(obj[i].checked) diff=parseInt(obj[i].value);
	var mis=parseInt(document.getElementById('mis').value);
	var far=parseInt(document.getElementById('far').value);
	var mxp=parseInt(document.getElementById('mxp').value);
	if(isNaN(mis)) {alert('输入数据不合法！');return;}
	if(isNaN(far)) {alert('输入数据不合法！');return;}
	if(isNaN(mxp)) {alert('输入数据不合法！');return;}
	var num,rank;
	if(songlist[title]==undefined||songlist[title][diff]==undefined)
	{
		var str='您在此处没有 "'+title+'" 的 '+['PST','PRS','FTR','BYD'][diff]+' 难度的谱面信息，需手动录入。\n如果只是打错了曲目名或者选错了难度，取消即可。';
		var r=confirm(str);
		if(!r) return;
		settab(1,'songupload');
		document.getElementById('songtitlea').value=title;
		var obja=document.getElementsByName("diffa");
		for(var i in obja) obja[i].checked=(obja[i].value==diff);
		document.getElementById('search').href='https://wiki.arcaea.cn/index.php?title=%E7%89%B9%E6%AE%8A:%E6%90%9C%E7%B4%A2&search='+title;
		backupload=true;
		return;
	}
	num=songlist[title][diff].num,rank=songlist[title][diff].rank;
	var newrec={'title':title,'diff':diff,'mis':mis,'far':far,'mxp':mxp};
	recentplay=newrec;
	var c=0;
	for(i in playlist) if(playlist[i].title==title&&playlist[i].diff==diff)
	{
		var ptt1=getptt(rank,num,playlist[i].mxp,playlist[i].mis,playlist[i].far);
		var ptt2=getptt(rank,num,mxp,mis,far);
		if(ptt2>=ptt1) playlist[i]=newrec;
		c=1;
	}
	if(c==0) playlist[playlist.length]=newrec;
	playlist.sort(function(a,b){
		var ptta=getptt(songlist[a.title][a.diff].rank,songlist[a.title][a.diff].num,a.mxp,a.mis,a.far);
		var pttb=getptt(songlist[b.title][b.diff].rank,songlist[b.title][b.diff].num,b.mxp,b.mis,b.far);
		return pttb-ptta;
	})
	save();
	show();
	document.getElementById('songtitle').value='';
	for(var i in obj) obj[i].checked=(obj[i].value==2);
	document.getElementById('mis').value='';
	document.getElementById('far').value='';
	document.getElementById('mxp').value='';
	document.getElementById('searcha').href='https://wiki.arcaea.cn/index.php?title=%E7%89%B9%E6%AE%8A:%E6%90%9C%E7%B4%A2&search=';
}
function uploadsong()
{
	var title=document.getElementById('songtitlea').value;
	var diff=null;var obj=document.getElementsByName("diffa");
	for(var i in obj) if(obj[i].checked) diff=parseInt(obj[i].value);
	var rank=parseFloat(document.getElementById('rank').value);
	var num=parseInt(document.getElementById('note').value); 
	if(isNaN(rank)) {alert('输入数据不合法！');return;}
	if(isNaN(num)) {alert('输入数据不合法！');return;}
	var r=confirm('请确认数据无误："'+title+'" 的 '+['PST','PRS','FTR','BYD'][diff]+' 难度的定数是 '+rank+'，物量是 '+num+'。');
	if(!r) return;
	if(songlist[title]==undefined) songlist[title]={};
	songlist[title][diff]={'rank':rank,'num':num};
	if(backupload)
	{
		settab(0,'index');
		uploadplay();
		backupload=false;
	}
	playlist.sort(function(a,b){
		var ptta=getptt(songlist[a.title][a.diff].rank,songlist[a.title][a.diff].num,a.mxp,a.mis,a.far);
		var pttb=getptt(songlist[b.title][b.diff].rank,songlist[b.title][b.diff].num,b.mxp,b.mis,b.far);
		return pttb-ptta;
	})
	document.getElementById('songtitlea').value='';
	for(var i in obj) obj[i].checked=(obj[i].value==2);
	document.getElementById('rank').value='';
	document.getElementById('note').value='';
	document.getElementById('search').href='https://wiki.arcaea.cn/index.php?title=%E7%89%B9%E6%AE%8A:%E6%90%9C%E7%B4%A2&search=';
	save();
	show();
}
function delsong()
{
	var title=document.getElementById('songtitleb').value;
	var diff=null;var obj=document.getElementsByName("diffb");
	for(var i in obj) if(obj[i].checked) diff=parseInt(obj[i].value);
	if(songlist[title]==undefined||songlist[title][diff]==undefined)
	{
		alert('此处没有 "'+title+'" 的 '+['PST','PRS','FTR','BYD'][diff]+' 难度的信息，请检查输入是否正确！');
		return;
	}
	var str='确认要删除 "'+title+'" 的 '+['PST','PRS','FTR','BYD'][diff]+' 难度的谱面信息吗？\n'+
	'定数：'+songlist[title][diff].rank+'；物量：'+songlist[title][diff].num;
	var r=confirm(str);
	if(!r) return;
	document.getElementById('songtitleb').value='';
	for(var i in obj) obj[i].checked=(obj[i].value==2);
	delete songlist[title][diff];
	if(Object.keys(songlist[title]).length==0) delete songlist[title];
	var a=-1;
	for(var i in playlist)
	{
		if(playlist[i].title==title&&playlist[i].diff==diff)
		{
			a=i;
		}
	}
	if(a!=-1)
	{
		for(i=parseInt(a);i<playlist.length-1;++i) playlist[i]=playlist[i+1];
		--playlist.length;
		if(recentplay.title==title&&recentplay.diff==diff) recentplay={};
	}
	save();show();
}
function delplay()
{
	var title=document.getElementById('songtitlec').value;
	var diff=null;var obj=document.getElementsByName("diffc");
	for(var i in obj) if(obj[i].checked) diff=parseInt(obj[i].value);
	var a=-1,b={};
	for(var i in playlist)
	{
		if(playlist[i].title==title&&playlist[i].diff==diff)
		{
			a=i,b=playlist[i];
		}
	}
	if(a==-1) {alert('没有 "'+title+'" 的 '+['PST','PRS','FTR','BYD'][diff]+' 难度的游玩记录。');return;}
	var num=songlist[title][diff].num;
	var r=confirm('确认删除 "'+title+'" 的 '+['PST','PRS','FTR','BYD'][diff]+' 难度的游玩记录吗？\n'+
		'PURE:'+(num-b.far-b.mis)+'(+'+b.mxp+') FAR:'+b.far+' LOST:'+b.mis+'\n得分：'+(Math.floor(1e7*((num-b.far-b.mis)+b.far/2)/num+b.mxp)));
	if(!r) return;
	for(i=parseInt(a);i<playlist.length-1;++i) playlist[i]=playlist[i+1];
	--playlist.length;
	if(recentplay.title==title&&recentplay.diff==diff) recentplay={};
	document.getElementById('songtitlec').value='';
	for(var i in obj) obj[i].checked=(obj[i].value==2);
	show();save();
}